package com.lion.common.spring.openapi;

import com.lion.common.spring.openapi.customizers.openapi.*;
import com.lion.common.spring.openapi.customizers.operation.*;
import com.lion.common.spring.openapi.httppack.ResponseWrapAnnotationProcessor;
import jakarta.annotation.PostConstruct;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author Chris Chan
 * Create On 2025/8/23 下午7:05
 * Use for: 动态分组OpenApi管理器
 * Explain:
 */
@Configuration
public class DynamicGroupedOpenApiManager {
    @Autowired
    List<GroupedOpenApi> groups;

    @Autowired
    private HttpResultOpenApiCustomizer httpResultOpenApiCustomizer;
    @Autowired
    private HttpRequestOpenApiCustomizer httpRequestOpenApiCustomizer;
    @Autowired
    private ValueResultOpenApiCustomizer valueResultOpenApiCustomizer;
    @Autowired
    private MultipartFileOpenApiCustomizer multipartFileOpenApiCustomizer;
    @Autowired
    private TagSortingOpenApiCustomizer tagSortingOpenApiCustomizer;
    @Autowired
    private TagPrefixOpenApiCustomizer tagPrefixOpenApiCustomizer;

    @Autowired
    private ResponseWrapAnnotationProcessor responseWrapAnnotationProcessor;
    @Autowired
    private ApiSignatureOperationCustomizer apiSignatureOperationCustomizer;
    @Autowired
    private HttpHeaderOperationCustomizer apiHttpHeaderOperationCustomizer;
    @Autowired
    private OperationIdOperationCustomizer operationIdCustomizer;
    @Autowired
    private SecurityOperationCustomizer securityOperationCustomizer;
    @Autowired
    private TagOrderOperationCustomizer tagOrderOperationCustomizer;


    /**
     * 调整
     */
    @PostConstruct
    public void adjust() {
        groups.forEach(group -> {
            group.addAllOpenApiCustomizer(List.of(
                    // 包装HttpResult
                    httpResultOpenApiCustomizer,
                    // 包装HttpRequest
                    httpRequestOpenApiCustomizer,
                    // 包装ValueResult
                    valueResultOpenApiCustomizer
                    // 为Tag添加排序
//                    tagSortingOpenApiCustomizer,
//                    // 为Tag添加前缀
//                    tagPrefixOpenApiCustomizer
                    // 包装MultipartFile 未生效
                    //multipartFileOpenApiCustomizer
            ));

            group.addAllOperationCustomizer(List.of(
                    // 响应包装
                    responseWrapAnnotationProcessor,
                    // 安全
                    securityOperationCustomizer,
                    // 签名
                    apiSignatureOperationCustomizer,
                    // 删除显式声明的HttpHeader参数
                    apiHttpHeaderOperationCustomizer,
                    // 操作ID
                    operationIdCustomizer,
                    // 为Tag添加排序
                    tagOrderOperationCustomizer
            ));
        });
    }
}
